{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3xW4O0zuKfWD"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from openai import OpenAI\n",
        "import rich\n",
        "from google.colab import userdata\n",
        "from google.colab import drive"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Note: we need to use models that support reasoning like o1 and o3-mini"
      ],
      "metadata": {
        "id": "ySQmmwK8biHh"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "api_key = userdata.get('OPENAI_API_KEY')\n",
        "MODEL = \"o3-mini\"\n",
        "\n",
        "openai = OpenAI(api_key=api_key)"
      ],
      "metadata": {
        "id": "vUydnLeLKs03"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Advanced reasoning and problem-solving models**"
      ],
      "metadata": {
        "id": "blCx6gu1Vl46"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Chat Completion API\n",
        "\n",
        "https://platform.openai.com/docs/guides/reasoning?api-mode=chat"
      ],
      "metadata": {
        "id": "5wlqKfVBKyjD"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Reasoning model takes time to respond."
      ],
      "metadata": {
        "id": "GYwnPiN5b8gf"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "prompt = \"\"\"\n",
        "Write a bash script that takes a matrix represented as a string with\n",
        "format '[1,2],[3,4],[5,6]' and prints the transpose in the same format.\n",
        "\"\"\"\n",
        "\n",
        "response = openai.chat.completions.create(\n",
        "    model=MODEL,\n",
        "    reasoning_effort=\"medium\",\n",
        "    messages=[\n",
        "        {\n",
        "            \"role\": \"user\",\n",
        "            \"content\": prompt\n",
        "        }\n",
        "    ]\n",
        ")\n",
        "\n",
        "print(rich.print(response.usage))\n",
        "print(response.choices[0].message.content)"
      ],
      "metadata": {
        "id": "dBrUkMCUKz0s",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "outputId": "ab11a918-e362-4cf0-92eb-8e0d07431ba6"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "\u001b[1;35mCompletionUsage\u001b[0m\u001b[1m(\u001b[0m\n",
              "    \u001b[33mcompletion_tokens\u001b[0m=\u001b[1;36m2384\u001b[0m,\n",
              "    \u001b[33mprompt_tokens\u001b[0m=\u001b[1;36m44\u001b[0m,\n",
              "    \u001b[33mtotal_tokens\u001b[0m=\u001b[1;36m2428\u001b[0m,\n",
              "    \u001b[33mcompletion_tokens_details\u001b[0m=\u001b[1;35mCompletionTokensDetails\u001b[0m\u001b[1m(\u001b[0m\n",
              "        \u001b[33maccepted_prediction_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n",
              "        \u001b[33maudio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m,\n",
              "        \u001b[33mreasoning_tokens\u001b[0m=\u001b[1;36m1472\u001b[0m,\n",
              "        \u001b[33mrejected_prediction_tokens\u001b[0m=\u001b[1;36m0\u001b[0m\n",
              "    \u001b[1m)\u001b[0m,\n",
              "    \u001b[33mprompt_tokens_details\u001b[0m=\u001b[1;35mPromptTokensDetails\u001b[0m\u001b[1m(\u001b[0m\u001b[33maudio_tokens\u001b[0m=\u001b[1;36m0\u001b[0m, \u001b[33mcached_tokens\u001b[0m=\u001b[1;36m0\u001b[0m\u001b[1m)\u001b[0m\n",
              "\u001b[1m)\u001b[0m\n"
            ],
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">CompletionUsage</span><span style=\"font-weight: bold\">(</span>\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">completion_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2384</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">prompt_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">44</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">total_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2428</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">completion_tokens_details</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">CompletionTokensDetails</span><span style=\"font-weight: bold\">(</span>\n",
              "        <span style=\"color: #808000; text-decoration-color: #808000\">accepted_prediction_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>,\n",
              "        <span style=\"color: #808000; text-decoration-color: #808000\">audio_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>,\n",
              "        <span style=\"color: #808000; text-decoration-color: #808000\">reasoning_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1472</span>,\n",
              "        <span style=\"color: #808000; text-decoration-color: #808000\">rejected_prediction_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>\n",
              "    <span style=\"font-weight: bold\">)</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">prompt_tokens_details</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">PromptTokensDetails</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">audio_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>, <span style=\"color: #808000; text-decoration-color: #808000\">cached_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span><span style=\"font-weight: bold\">)</span>\n",
              "<span style=\"font-weight: bold\">)</span>\n",
              "</pre>\n"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "None\n",
            "Below is one acceptable solution. Save the script as, for example, transpose.sh, give it executable permissions (chmod +x transpose.sh), then run it passing the matrix string as its first argument.\n",
            "\n",
            "Below is the complete script:\n",
            "\n",
            "------------------------------------------------\n",
            "#!/bin/bash\n",
            "# Check that we got exactly one argument.\n",
            "if [ \"$#\" -ne 1 ]; then\n",
            "  echo \"Usage: $0 '[1,2],[3,4],[5,6]'\"\n",
            "  exit 1\n",
            "fi\n",
            "\n",
            "matrix_str=\"$1\"\n",
            "\n",
            "# Convert the single input string into multiple lines (one per row) by:\n",
            "# • Removing any leading \"[\" and trailing \"]\"\n",
            "# • Replacing \"],[\" with a newline.\n",
            "rows=($(echo \"$matrix_str\" | sed 's/^\\[//; s/\\]$//; s/\\],\\[/\\n/g'))\n",
            "\n",
            "# Number of rows in the matrix.\n",
            "num_rows=${#rows[@]}\n",
            "\n",
            "# Determine the number of columns from the first row.\n",
            "IFS=',' read -ra first_row <<< \"${rows[0]}\"\n",
            "num_cols=${#first_row[@]}\n",
            "\n",
            "# Create an array to hold the transposed rows.\n",
            "declare -a transposed_rows\n",
            "\n",
            "# Loop over each column index (which will become rows in the transpose)\n",
            "for (( col=0; col<num_cols; col++ )); do\n",
            "  # Build a new row for the transposed matrix.\n",
            "  new_row=\"[\"\n",
            "  \n",
            "  # For each row in the original matrix, extract the current element.\n",
            "  for (( row=0; row<num_rows; row++ )); do\n",
            "    IFS=',' read -ra elements <<< \"${rows[row]}\"\n",
            "    new_row+=\"${elements[col]}\"\n",
            "    # Add a comma if this is not the last element in the row.\n",
            "    if [ $row -lt $((num_rows - 1)) ]; then\n",
            "      new_row+=\",\"\n",
            "    fi\n",
            "  done\n",
            "\n",
            "  new_row+=\"]\"\n",
            "  transposed_rows+=(\"$new_row\")\n",
            "done\n",
            "\n",
            "# Join the transposed rows with commas.\n",
            "result=$(IFS=, ; echo \"${transposed_rows[*]}\")\n",
            "echo \"$result\"\n",
            "\n",
            "------------------------------------------------\n",
            "\n",
            "How the script works:\n",
            "\n",
            "1. The script takes a single argument. For example, if you run:\n",
            "   ./transpose.sh '[1,2],[3,4],[5,6]'\n",
            "   Then matrix_str becomes \"[1,2],[3,4],[5,6]\".\n",
            "\n",
            "2. The sed command removes the starting \"[\" and ending \"]\" and then replaces \"],[\" with newline, so that the input gets split into:\n",
            "   1. 1,2\n",
            "   2. 3,4\n",
            "   3. 5,6\n",
            "\n",
            "3. It then determines the number of columns (in this case 2) by reading the first row.\n",
            "\n",
            "4. For each column position, it loops over all rows to build a new row consisting of that column’s elements.\n",
            "\n",
            "5. Finally, it prints the transposed matrix in the same format, e.g.:\n",
            "   [1,3,5],[2,4,6]\n",
            "\n",
            "This solution meets the problem requirements.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Responses API\n",
        "\n",
        "https://platform.openai.com/docs/guides/reasoning?api-mode=responses"
      ],
      "metadata": {
        "id": "tCzSosP5K-JR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "prompt = \"\"\"\n",
        "Write a bash script that takes a matrix represented as a string with\n",
        "format '[1,2],[3,4],[5,6]' and prints the transpose in the same format.\n",
        "\"\"\"\n",
        "\n",
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    reasoning={\"effort\": \"medium\"}, # reasoning_effort now become reasoning and accept a dictionary\n",
        "    input=[\n",
        "        {\n",
        "            \"role\": \"user\",\n",
        "            \"content\": prompt\n",
        "        }\n",
        "    ]\n",
        ")\n",
        "\n",
        "rich.print(response.usage)\n",
        "print(response.output_text)"
      ],
      "metadata": {
        "id": "gUjC_PQCcETm",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "outputId": "d76f2fae-5b18-48c0-c78e-b7cf361204d5"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "\u001b[1;35mResponseUsage\u001b[0m\u001b[1m(\u001b[0m\n",
              "    \u001b[33minput_tokens\u001b[0m=\u001b[1;36m103\u001b[0m,\n",
              "    \u001b[33minput_tokens_details\u001b[0m=\u001b[1;35mInputTokensDetails\u001b[0m\u001b[1m(\u001b[0m\u001b[33mcached_tokens\u001b[0m=\u001b[1;36m0\u001b[0m\u001b[1m)\u001b[0m,\n",
              "    \u001b[33moutput_tokens\u001b[0m=\u001b[1;36m1880\u001b[0m,\n",
              "    \u001b[33moutput_tokens_details\u001b[0m=\u001b[1;35mOutputTokensDetails\u001b[0m\u001b[1m(\u001b[0m\u001b[33mreasoning_tokens\u001b[0m=\u001b[1;36m1152\u001b[0m\u001b[1m)\u001b[0m,\n",
              "    \u001b[33mtotal_tokens\u001b[0m=\u001b[1;36m1983\u001b[0m\n",
              "\u001b[1m)\u001b[0m\n"
            ],
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">ResponseUsage</span><span style=\"font-weight: bold\">(</span>\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">input_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">103</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">input_tokens_details</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">InputTokensDetails</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">cached_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span><span style=\"font-weight: bold\">)</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">output_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1880</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">output_tokens_details</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">OutputTokensDetails</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">reasoning_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1152</span><span style=\"font-weight: bold\">)</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">total_tokens</span>=<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1983</span>\n",
              "<span style=\"font-weight: bold\">)</span>\n",
              "</pre>\n"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Below is one solution in a bash script. Save it, for example, as transpose_matrix.sh, give it execute permission (chmod +x transpose_matrix.sh), and then run it with the matrix string as its only argument.\n",
            "\n",
            "#!/bin/bash\n",
            "# This script expects a single argument in the form:\n",
            "#   \"[1,2],[3,4],[5,6]\"\n",
            "# It will compute the transpose and output it in the same format.\n",
            "#\n",
            "# For example:\n",
            "#   ./transpose_matrix.sh \"[1,2],[3,4],[5,6]\"\n",
            "# will output:\n",
            "#   [1,3,5],[2,4,6]\n",
            "\n",
            "if [ \"$#\" -ne 1 ]; then\n",
            "  echo \"Usage: $0 \\\"[row1],[row2],...\\\"\"\n",
            "  exit 1\n",
            "fi\n",
            "\n",
            "input=\"$1\"\n",
            "# Remove any spaces from the string (optional)\n",
            "input=$(echo \"$input\" | tr -d ' ')\n",
            "\n",
            "# Convert the input string into rows.\n",
            "# We replace the \"],[\" separator with a space and then remove the remaining brackets.\n",
            "# After this, each row is a comma-separated list of elements.\n",
            "rows=$(echo \"$input\" | sed 's/\\],\\[/ /g; s/^\\[//; s/\\]$//; s/\\[//g; s/\\]//g')\n",
            "\n",
            "# Read the rows into an array.\n",
            "i=0\n",
            "declare -a matrix\n",
            "for row in $rows; do\n",
            "    matrix[$i]=\"$row\"\n",
            "    i=$((i+1))\n",
            "done\n",
            "\n",
            "rows_count=${#matrix[@]}\n",
            "\n",
            "# Get the number of columns by splitting the first row by comma.\n",
            "IFS=',' read -r -a first_row_elements <<< \"${matrix[0]}\"\n",
            "cols_count=${#first_row_elements[@]}\n",
            "\n",
            "# Build the transpose.\n",
            "# For each column in the original matrix, we build a row in the transposed matrix.\n",
            "result=\"\"\n",
            "for (( c=0; c<cols_count; c++ )); do\n",
            "    transposed_row=\"\"\n",
            "    for (( r=0; r<rows_count; r++ )); do\n",
            "        # Split the r-th row into tokens\n",
            "        IFS=',' read -r -a tokens <<< \"${matrix[r]}\"\n",
            "        # Append the element from column c of row r\n",
            "        if [ $r -eq 0 ]; then\n",
            "            transposed_row=\"${tokens[c]}\"\n",
            "        else\n",
            "            transposed_row=\"${transposed_row},${tokens[c]}\"\n",
            "        fi\n",
            "    done\n",
            "    # Wrap the transposed row in brackets.\n",
            "    transposed_row=\"[${transposed_row}]\"\n",
            "    # Append the row to the result, adding a comma if not the first row.\n",
            "    if [ -z \"$result\" ]; then\n",
            "        result=\"$transposed_row\"\n",
            "    else\n",
            "        result=\"${result},${transposed_row}\"\n",
            "    fi\n",
            "done\n",
            "\n",
            "echo \"$result\"\n",
            "\n",
            "Explanation:\n",
            "\n",
            "1. The script first removes spaces and then uses sed to replace the \"],[\"\n",
            "   delimiters by a space. It also strips the extra square brackets.\n",
            "2. Each row is stored as a comma‐separated string in the array “matrix.”\n",
            "3. The number of columns is determined by splitting the first row on commas.\n",
            "4. The nested loops go over each column and then over each row to collect the element\n",
            "   at that position, building the transposed rows.\n",
            "5. Finally, the transposed matrix is printed in the same “[a,b]” comma-separated format.\n",
            "\n",
            "This solution uses only common Unix utilities and bash built‐ins.\n"
          ]
        }
      ]
    }
  ]
}